package com.xxx.modules.controller;;
import com.xxx.modules.service.NewsService;
import com.xxx.modules.entity.News;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * news
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-02-11
 */
@RestController
@RequestMapping("/common/news")
@Api(tags="news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
    *  获取所有新闻公告
    * @param pageNum
    * @param pageSize
    * @param news
    * @return
    */

    @GetMapping("/getNewsInfo")
    @ApiOperation("获取所有新闻公告")
    public Result getNewsInfo(News news, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                              @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest request){
        PageInfo<News> data = newsService.selectNewsInfo(news,pageNum,pageSize);
        String imageUrl = request.getScheme() + "://" +
                request.getServerName() + ":" +
                request.getServerPort() + "/document/";
        return ResultUtil.successForDataAndImage(1,"成功",data, imageUrl);
    }

    /**
    * 保存新闻公告
    * @param news
    * @return
    */
    @PostMapping("/saveNewsInfo")
    @ApiOperation("保存新闻公告")
    public Result saveNewsInfo(@RequestBody News news){
        newsService.saveNewsInfo(news);
        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 更新新闻公告
     * @param news
     * @return
     */

    @PutMapping("/updateNewsInfo")
    @ApiOperation("更新新闻公告")
    public Result updateNewsInfo(@RequestBody News news){
        newsService.updateNewsInfo(news);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除新闻公告
     * @param id
     * @return
     */
    @DeleteMapping("/delNewsInfo")
    @ApiOperation("根据id删除新闻公告")
    public Result delNewsInfo(Integer id){
        newsService.delNewsInfo(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合批量删除新闻公告
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatchNewsInfo")
    @ApiOperation("根据id集合批量删除新闻公告")
    public Result delBatchNewsInfo(String idList){
        newsService.delBatchNewsInfo(idList);
        return ResultUtil.success(1,"成功",null);
    }

}